package com.dimples.dd.system.controller;

import com.dimples.dd.common.constant.DDServerConstant;
import com.dimples.dd.common.model.Option;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.log.core.annotation.ApiLog;
import com.dimples.dd.log.core.enums.OperateTypeEnum;
import com.dimples.dd.system.model.form.PostForm;
import com.dimples.dd.system.model.query.PostPageQuery;
import com.dimples.dd.system.model.vo.PostPageVO;
import com.dimples.dd.system.service.SystemPostService;
import com.dimples.dd.web.core.annotation.PreventDuplicateResubmit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/7/3
 */
@Tag(name = "06.岗位接口")
@RestController
@Validated
@RequestMapping("post")
@RequiredArgsConstructor
public class SysPostController {

    private final SystemPostService systemPostService;

    @Operation(summary = "岗位分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:post:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public PageResult<PostPageVO> getPostPage(
            @ParameterObject PostPageQuery queryParams
    ) {
        return this.systemPostService.getPostPage(queryParams);
    }

    @Operation(summary = "岗位下拉列表")
    @GetMapping("/options")
    public CommonResult<List<Option<Long>>> listPostOptions() {
        List<Option<Long>> list = this.systemPostService.listPostOptions();
        return CommonResult.success(list);
    }

    @Operation(summary = "岗位表单数据")
    @GetMapping("/{postId}/form")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<PostForm> getPostForm(
            @Parameter(description = "角色ID") @PathVariable Long postId
    ) {
        PostForm roleForm = this.systemPostService.getPostForm(postId);
        return CommonResult.success(roleForm);
    }

    @Operation(summary = "新增岗位")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:post:create')")
    @PreventDuplicateResubmit
    @ApiLog(operateType = OperateTypeEnum.CREATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> createPost(@Valid @RequestBody PostForm postForm) {
        boolean result = this.systemPostService.createPost(postForm);
        return CommonResult.success(result);
    }

    @Operation(summary = "修改岗位")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:post:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> updatePost(@Valid @RequestBody PostForm postForm) {
        boolean result = this.systemPostService.updatePost(postForm);
        return CommonResult.success(result);
    }

    @Operation(summary = "删除岗位")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:post:delete')")
    @ApiLog(operateType = OperateTypeEnum.DELETE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> deletePost(
            @Parameter(description = "删除角色，多个以英文逗号(,)分割") @NotNull(message = "不能为空") @PathVariable String ids
    ) {
        boolean result = this.systemPostService.deletePost(ids);
        return CommonResult.success(result);
    }
}
